<template>
	<view class="page">
		<view class="head">
			<image src="/static/images/img_circle.png" mode="widthFix"></image>
			<view class="user">
				<image class="avatar" :src="user.avatar || '/static/images/default_avatar.png'" mode="aspectFill"></image>
				<view class="name">{{ user.nickName }}</view>
				<view @click="add(user.isLike == 0 ? 1 : 0)" :class="user.isLike == 1 ? 'add gray' : 'add'">{{ user.isLike == 1 ? '已关注' : '关注' }}</view>
			</view>
		</view>
		<view class="list">
			<view class="item" v-for="(item, index) in list" :key="index" @click="to('/pages/CommunityPages/details/main?id=' + item.id)">
				<view class="top">
					<view><image class="avatar" :src="item.avatar || '/static/images/default_avatar.png'" mode="aspectFill"></image></view>
					<view class="name-box">
						<p class="name">{{ item.nickName || '' }}</p>
						<p class="time">{{ item.createTime }}</p>
					</view>
					<view class="solution" v-if="item.helpStatus == 1">已解决</view>
				</view>
				<view class="content-box">
					<text class="good" v-if="item.publishStatus == 2">精</text>
					<text class="stick" v-if="item.publishStatus == 1">顶</text>
					<text class="ask" v-if="item.publishType == 1">问</text>
					<text v-if="item.publishSubject" class="title">【{{ item.publishSubject }}】</text>
					<text class="content">{{ item.publishContent || '' }}</text>
				</view>
				<view v-if="item.showImage" class="img-box" @click.stop>
					<img class="img" v-for="(x, y) in item.imgList" :key="y" :src="x" @click="preview(x, item.imgList)" />
				</view>
				<view class="btn-primary" v-if="item.imgList.length > 0" @click.stop="changeImageStatus(index)">{{ item.showImage ? '隐藏图片' : '查看图片' }}</view>
				<view class="bottom">
					<view class="disease" @click.stop="to('/pages/CommunityPages/circleDetails/main?id=' + item.groupId)">{{ item.groupName }}</view>
					<view class="right-box">
						<view class="operation">
							<image class="icon" src="@/static/images/ic_comment.png"></image>
							<p class="count">{{ item.commentsAmount }}</p>
						</view>
						<view class="operation">
							<image class="icon" src="@/static/images/ic_like.png"></image>
							<p class="count">{{ item.collectQuantity }}</p>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
import { addFocus, cancelFocus } from '@/apis/request';

export default {
	data() {
		return {
			loginUserId: null,
			userId: null,
			user: {},
			finished: false,
			pageNo: 1,
			total: 0,
			list: []
		};
	},
	onLoad(options) {
		this.loginUserId = uni.getStorageSync('userInfo').id || null;
		this.userId = options.id;
		this.init();
	},
	onPullDownRefresh() {
		this.init();
	},
	onReachBottom() {
		this.getUserlist();
	},
	methods: {
		init() {
			this.$api.http(
				'/user/medicalUser/api/n/getUserInfo',
				'GET',
				{
					checkUserId: this.loginUserId,
					userId: this.userId
				},
				res => {
					this.user = res.data.result;
				}
			);
			this.finished = false;
			this.pageNo = 1;
			this.list = [];
			this.getUserlist();
		},
		getUserlist() {
			if (!this.finished) {
				this.$api.http(
					'/user/userPublishInfo/api/n/getPostSpaceByUserId',
					'GET',
					{
						pageNo: this.pageNo++,
						pageSize: 10,
						userId: this.userId
					},
					res => {
						if (res.data.result.records.length == 0) {
							this.finished = true;
						}
						this.list = this.list.concat(
							...res.data.result.records.map(item => {
								return {
									...item,
									showImage: false,
									imgList: item.imageUrl ? item.imageUrl.split(',') : []
								};
							})
						);
						uni.stopPullDownRefresh();
					}
				);
			}
		},
		add(type) {
			if (type == 0) {
				cancelFocus({
					attentionId: this.userId,
					attentionType: 2,
					userId: this.loginUserId
				});
			} else {
				addFocus({
					attentionId: this.userId,
					attentionType: 2,
					userId: this.loginUserId
				});
			}
			setTimeout(() => {
				this.init();
			}, 100);
		},
		preview(current, imgList) {
			this.$Imgpreview(current, imgList);
		},
		changeImageStatus(index) {
			this.list[index].showImage = !this.list[index].showImage;
		},
		to(url) {
			uni.navigateTo({
				url
			});
		}
	}
};
</script>

<style lang="scss" scoped>
.page {
	width: 100%;
	background-color: #f1efef;

	.head {
		position: relative;

		image {
			display: block;
			width: 100%;
		}

		.user {
			display: flex;
			flex-direction: row;
			position: absolute;
			bottom: -40rpx;
			left: 30rpx;

			.avatar {
				width: 120rpx;
				height: 120rpx;
				border-radius: 50%;
			}

			.name {
				margin: 30rpx 20rpx 0;
				font-size: 30rpx;
				color: #ffffff;
			}

			.add {
				width: 100rpx;
				height: 40rpx;
				margin: 30rpx 0rpx 0rpx;
				background: #ffffff;
				border: 1rpx solid #32C59A;
				border-radius: 25rpx;
				font-size: 22rpx;
				color: #32C59A;
				line-height: 40rpx;
				text-align: center;
			}

			.gray {
				color: #999999;
				border: 1rpx solid #dcdcdc;
			}
		}
	}

	.list {
		padding: 0 30rpx;
		padding-top: 60rpx;
		box-sizing: border-box;
		background-color: #fff;

		.item {
			padding: 20rpx 0;
			box-sizing: border-box;
			border-bottom: 1px solid #f1f1f1;

			.top {
				position: relative;
				display: flex;
				align-items: center;
				align-content: center;

				.avatar {
					width: 80rpx;
					height: 80rpx;
					border-radius: 50%;
				}

				.name-box {
					margin-left: 15rpx;
					display: flex;
					align-items: center;
					align-content: center;
					flex-direction: column;

					.name {
						width: 100%;
						font-size: 28rpx;
						text-align: left;
					}

					.time {
						color: #aaa;
						font-size: 24rpx;
					}
				}

				.solution {
					position: absolute;
					right: 0;
					color: #ff290b;
					font-size: 26rpx;
					font-weight: bold;
					transform: rotate(45deg);
				}

				.delete {
					position: absolute;
					top: 0;
					right: 0;
					width: 28.5rpx;
					height: 33.75rpx;
				}
			}

			.reward {
				margin: 5px 0;
				font-size: 28rpx;
				color: #e5ae54;
			}

			.content-box {
				.ask {
					margin-right: 5rpx;
					padding: 0 5rpx;
					color: #f83f06;
					font-size: 24rpx;
					border: 1rpx solid #f83f06;
					background-color: #fff3f4;
					border-radius: 5rpx;
				}

				.stick {
					margin-right: 5rpx;
					padding: 0 5rpx;
					color: #1dda61;
					font-size: 24rpx;
					border: 1rpx solid #1dda61;
					background-color: #e6f9ed;
					border-radius: 5rpx;
				}

				.good {
					margin-right: 5rpx;
					padding: 0 5rpx;
					color: #07a0f8;
					font-size: 24rpx;
					border: 1rpx solid #07a0f8;
					background-color: #e7f4fb;
					border-radius: 5rpx;
				}

				.title {
					color: #333333;
					font-size: 30rpx;
				}

				.content {
					color: #333333;
					font-size: 30rpx;
				}
			}

			.img-box {
				display: flex;
				flex-wrap: wrap;

				.img {
					margin: 10rpx 10rpx 0 0;
					width: 222rpx;
					height: 222rpx;
					border-radius: 10rpx;

					&:nth-child(3n) {
						margin-right: 0;
					}
				}
			}

			.bottom {
				margin-top: 20rpx;
				display: flex;
				align-items: center;
				align-content: center;
				justify-content: space-between;

				.disease {
					padding: 10rpx 25rpx;
					color: #f48712;
					font-size: 26rpx;
					border-radius: 30rpx;
					background-color: #fef5ee;
				}

				.right-box {
					display: flex;
					align-items: center;
					align-content: center;

					.operation {
						display: flex;
						align-items: center;
						align-content: center;

						.icon {
							width: 40rpx;
							height: 40rpx;
						}

						.count {
							margin-left: 8rpx;
							font-size: 24rpx;
						}

						&:last-child {
							margin-left: 30rpx;
						}
					}
				}
			}
		}
	}

	.btn-primary {
		height: 60rpx;
		width: 200rpx;
		margin: 0 auto;
		margin-top: 20rpx;
		line-height: 60rpx;
		text-align: center;
		background: #32C59A;
		color: #ffffff;
		font-size: 28rpx;
		border-radius: 100rpx;
	}
}
</style>
